package com.kd.juhaowan.ui;

import android.app.AlertDialog;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import androidx.annotation.Nullable;


import com.hjq.http.EasyHttp;
import com.hjq.http.listener.HttpCallbackProxy;
import com.hjq.toast.ToastUtils;
import com.kd.juhaowan.R;
import com.kd.juhaowan.http.api.PhoneAccountListApi;
import com.kd.juhaowan.http.api.PhoneCodeApi;
import com.kd.juhaowan.http.model.HttpData;
import com.kd.juhaowan.http.model.HttpListData;
import com.kd.juhaowan.model.LoginUser;
import com.kd.juhaowan.utils.MsgCountDown;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class ForgetActivity extends BaseActivity {

    private int logtype = 1;



    @BindView(R.id.regist_phone_code_get)
    TextView loginCode;
    @BindView(R.id.login_account_account)
    EditText registAccount;
    @BindView(R.id.login_account_pwd)
    EditText registPwd;

//    @BindView(R.id.actvity_back)
//    LinearLayout backLayout;


    @Override
    protected void reInitDate() {

    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        setContentView(R.layout.activity_account_login);

    }

    @Override
    protected int getViewLayoutResourceId() {
        return R.layout.activity_forget;
    }

//    @OnClick(R.id.login_account_forget)
//    public void onForgetClick(View view){
//
//    }

    @OnClick(R.id.login_confirm)
    public void onLoginClick(View view){
        String phone = registAccount.getText().toString().trim();
        String code = registPwd.getText().toString().trim();
        if(TextUtils.isEmpty(phone)){
            ToastUtils.show("请输入手机号码");
        }else if(TextUtils.isEmpty(code)){
            ToastUtils.show("请输入验证码");
        }else {
            EasyHttp.post(this).api(new PhoneAccountListApi(phone,code,"retrieve_password","get_code_retrieve_password")).request(new HttpCallbackProxy<HttpListData<LoginUser>>(this){
                @Override
                public void onHttpSuccess(HttpListData<LoginUser> result, boolean cache) {
                    super.onHttpSuccess(result, cache);
//                        LogUtil.debugLog(result.getData().getAccess_token());
//                        Toast.makeText(AccountLoginActivity.this,"登录成功",Toast.LENGTH_SHORT).show();
                    List<LoginUser> users = result.getData().getList();
                    if(users==null||users.size()==0){
//                        ToastUtils.show("当前手机号未绑定账号");
                        AlertDialog dialog = new AlertDialog.Builder(ForgetActivity.this).create();
                        dialog.show();
                        return;
                    }
//                        if(users.size()==1){
//
//                        }
                    for (int i=0;i<users.size();i++) {
                        users.get(i).setToken(result.getData().getToken());
                    }
                    ArrayList<LoginUser> buildrUsers = new ArrayList<>();
                    buildrUsers.addAll(users);
                    Intent intent=new Intent(ForgetActivity.this,PhoneLoginAccountActivity.class);
                    intent.putExtra("user",buildrUsers);
                    intent.putExtra("isForget",true);
                    intent.putExtra("phone",phone);
                    startActivity(intent);

                }
            });
        }

//        startActivity(new Intent(ForgetActivity.this,ForgetFindActivity.class));
//        AlertDialog dialog = new AlertDialog.Builder(ForgetActivity.this).create();
//        dialog.show();
    }

    @OnClick(R.id.actvity_back)
    public void gotoRegist(View view){
        finish();
    }
    @OnClick(R.id.regist_phone_code_get)
    public void getCode(View view){
        String account = registAccount.getText().toString().trim();
        if(TextUtils.isEmpty(account)){
            ToastUtils.show("请先输入手机号");
            return;
        }
        EasyHttp.post(this).api(new PhoneCodeApi(account,"get_code_retrieve_password")).request(new HttpCallbackProxy<HttpData<LoginUser>>(this){
            @Override
            public void onHttpSuccess(HttpData<LoginUser> result) {
                super.onHttpSuccess(result);
                ToastUtils.show("获取验证码成功");
                MsgCountDown.setCurrentTime();
                updateSmsCount();
            }
        });
    }

    private void updateSmsCount() {
        if (isFinishing()||isDestroyed()) {
            return;
        }

        int value = (int) ((System.currentTimeMillis() - MsgCountDown.getLastTime()) / 1000);
        if (value > 59) {
            loginCode.setEnabled(true);
            loginCode.setText("重新发送");
//            loginCode.setTextColor(Color.parseColor("#ffffff"));
        } else {
//            reGetCode.setTextColor(Color.parseColor("#a39dce"));
//			reGetCode.setTextColor(ViewUtil.getColorRs(activity,"fjd_white"));
            loginCode.setEnabled(false);
            loginCode.setText((60 - value) + "s");
            loginCode.postDelayed(new Runnable() {

                @Override
                public void run() {
                    updateSmsCount();
                }
            }, 1000);
        }
    }
}
